﻿using CraftParameterConfig.Domain.Entity;
using CraftParameterConfig.Domain.Interfaces;
using CraftParameterConfig.Infrastructure.Common.Enums;
using CraftParameterConfig.Services.Interfaces;
using CraftParameterConfig.Services.Models;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace CraftParameterConfig.Services.Data
{
   public  class TableFieldService : ITableFieldService
    {
        ITableNameRespository tableNameRespository;
        ITableIndexRespository tableIndexRespository;
        ITableDataRespository tableDataRespository;
        public TableFieldService(ITableNameRespository tableNameRespository, ITableIndexRespository tableIndexRespository, ITableDataRespository tableDataRespository)
        {
            this.tableNameRespository = tableNameRespository;
            this.tableIndexRespository = tableIndexRespository;
            this.tableDataRespository = tableDataRespository;
        }
        public string AddData(TableDataDTO tableDataDTO)
        {
            TableDataConfig tableDataConfig = new TableDataConfig(
                Guid.Parse(tableDataDTO.MainId),
                tableDataDTO.Name,
                (ValueDataType)Enum.Parse(typeof(ValueDataType),tableDataDTO.DataType),
                tableDataDTO.NeedBaseValue == "True" ? true : false,
                tableDataDTO.NeedLHValue == "True" ? true : false,
                tableDataDTO.NeedLLHHValue == "True" ? true : false,
                tableDataDTO.Order_Index);
            tableDataConfig.CREATE_USER = "SC";

            try
            {
                tableDataRespository.Add(tableDataConfig);
                MessageBox.Show("新增数据成功!");
                return tableDataConfig.CREATE_TIME.ToString();
            }
            catch (Exception)
            {
                throw;
            }
        }

        public string AddData(List<TableDataDTO> tableDataDTOs)
        {
            List<TableDataConfig> list = new List<TableDataConfig>();
            DateTime createTime = DateTime.Now;
            
            foreach (var item in tableDataDTOs)
            {
                TableDataConfig tableDataConfig = new TableDataConfig(
                   Guid.Parse(item.MainId),
                   item.Name,
                   (ValueDataType)Enum.Parse(typeof(ValueDataType), item.DataType),
                   item.NeedBaseValue == "TRUE" ? true : false,
                   item.NeedLHValue == "TRUE" ? true : false,
                   item.NeedLLHHValue == "TRUE" ? true : false,
                   item.Order_Index);
                tableDataConfig.CREATE_USER = "SC";
                tableDataConfig.CREATE_TIME = createTime;

                list.Add(tableDataConfig);
            }

            try
            {
                tableDataRespository.AddRange(list);
                MessageBox.Show("新增数据成功!");
                return createTime.ToString();
            }
            catch (Exception)
            {
                throw;
            }
        }

        public string AddIndex(TableIndexDTO tableIndexDTO)
        {
            TableIndexConfig tableIndexConfig = new TableIndexConfig(Guid.Parse(tableIndexDTO.MainId),
                tableIndexDTO.Name,
                (IndexDataType)Enum.Parse(typeof(IndexDataType), tableIndexDTO.DataType),
                (DataCondition)Enum.Parse(typeof(DataCondition), tableIndexDTO.DataCondition),
                tableIndexDTO.Order_Index);
            tableIndexConfig.CREATE_USER = "SC";

            try
            {
                tableIndexRespository.Add(tableIndexConfig);
                MessageBox.Show("新增索引字段成功!");
                return tableIndexConfig.CREATE_TIME.ToString();
            }
            catch (Exception)
            {
                throw;
            }
        }

        public string AddIndex(List<TableIndexDTO> tableIndexDTOs)
        {
            List<TableIndexConfig> list = new List<TableIndexConfig>();
            DateTime createTime = DateTime.Now;

            foreach (var item in tableIndexDTOs)
            {
                TableIndexConfig tableIndexConfig = new TableIndexConfig(Guid.Parse(item.MainId),
                 item.Name,
                 (IndexDataType)Enum.Parse(typeof(IndexDataType), item.DataType),
                 (DataCondition)Enum.Parse(typeof(DataCondition), item.DataCondition),
                 item.Order_Index);
                tableIndexConfig.CREATE_USER = "SC";

                list.Add(tableIndexConfig);
            }

            try
            {
                tableIndexRespository.AddRange(list);
                MessageBox.Show("新增值字段成功!");
                return createTime.ToString();
            }
            catch (Exception)
            {
                throw;
            }
        }

        public DataTable GetDataTable(string guidid)
        {
            return tableDataRespository.GetTable(string.Format("where t.MAINID='{0}'", guidid));
        }

        public DataTable GetIndexTable(string guidid)
        {
            return tableIndexRespository.GetTable(string.Format("where t.MAINID='{0}'", guidid));
        }

        public DataTable GetNameTable()
        {
            return tableNameRespository.GetTable("");
        }

        public void UpdateIndex(TableIndexDTO tableIndexDTO)
        {
            TableIndexConfig tableIndexConfig= new TableIndexConfig(Guid.Parse(tableIndexDTO.MainId),
                 tableIndexDTO.Name,
                 (IndexDataType)Enum.Parse(typeof(IndexDataType), tableIndexDTO.DataType),
                 (DataCondition)Enum.Parse(typeof(DataCondition), tableIndexDTO.DataCondition),
                 tableIndexDTO.Order_Index);
            tableIndexConfig.ID = tableIndexDTO.ID;

            tableIndexRespository.Update(tableIndexConfig);
            MessageBox.Show("修改值字段成功!");
        }

        public void UpdateIndex(List<TableIndexDTO> tableIndexDTOs)
        {
            List<TableIndexConfig> tableIndexConfigs = new List<TableIndexConfig>();
            foreach (var item in tableIndexDTOs)
            {
                TableIndexConfig tableIndexConfig = new TableIndexConfig(Guid.Parse(item.MainId),
                  item.Name,
                  (IndexDataType)Enum.Parse(typeof(IndexDataType), item.DataType),
                  (DataCondition)Enum.Parse(typeof(DataCondition), item.DataCondition),
                  item.Order_Index);
                tableIndexConfig.ID = item.ID;

                tableIndexConfigs.Add(tableIndexConfig);
            }
            tableIndexRespository.Update(tableIndexConfigs);
            MessageBox.Show("修改值字段成功!");
        }
    }
}
